home *** CD-ROM | disk | FTP | other *** search
-
- %{
-
- #import <ansi/stdlib.h>
- #import <ansi/string.h>
- #import <ansi/stdio.h>
- #import <ansi/ctype.h>
- #import <ansi/math.h>
-
- #import "../Tree.h"
-
- extern void mmaPrepareTextToScan(NXStream *text);
- extern yylex(), yyparse();
-
- static void yyerror(char *s);
- static NXZone *treeZone;
- static Properties *treeProps;
- static Tree *resultTree;
- static Tree *mmaMakeLeaf(char *label);
- static Tree *mmaMakeTree(char *label, List *children);
- static List *mmaAddChild(Tree *tree, List *children);
-
- %}
-
- %union{
- Tree *tree;
- char *name;
- List *list;
- }
-
- %token <name> NAME
- %token COMMA
- %left LB RB
-
- %type <tree> mmatree mmadoc
- %type <list> childrenlist
-
- %%
-
- mmadoc: mmatree
- {
- resultTree = $1;
- }
- ;
-
- mmatree: NAME
- {
- $$ = mmaMakeLeaf($1);
- }
- | LB NAME COMMA childrenlist RB
- {
- $$ = mmaMakeTree($2, $4);
- }
- | /* empty */
- {
- $$ = nil;
- }
- ;
-
- childrenlist: mmatree
- {
- $$ = mmaAddChild($1, nil);
- }
- | mmatree COMMA childrenlist
- {
- $$ = mmaAddChild($1, $3);
- }
- ;
-
- %%
-
- static Tree *mmaMakeLeaf(char *label)
- {
- return [[Tree allocFromZone:treeZone] initLabel:label props:treeProps];
- }
-
- static Tree *mmaMakeTree(char *label, List *children)
- {
- Tree *tree, *child;
- int i;
-
- tree = [[Tree allocFromZone:treeZone] initLabel:label props:treeProps];
- for(i = 0; i < [children count]; i++){
- child = (Tree *)[children objectAt:i];
- [tree addTree:child];
- }
- [children free];
- return tree;
- }
-
- static List *mmaAddChild(Tree *tree, List *children)
- {
- List *list;
-
- if(children){
- [children addObject:tree];
- return children;
- } else {
- list = [[List alloc] init];
- [list addObject:tree];
- return list;
- }
- }
-
- id createMMATree(NXStream *stream, Properties *props, NXZone *zone)
- {
- int r;
-
- resultTree = nil;
- mmaPrepareTextToScan(stream);
- treeZone = zone;
- treeProps = props;
- r = yyparse();
- return resultTree;
- }
-
- static void yyerror(char *s)
- {
- printf("parser error:%s\n",s);
- }
-
-